<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title>HTML代码预览与截图</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
    <script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
    <style>
      html,
      body {
        height: 100%;
        margin: 0;
        padding: 0;
        background: #f5f5f5;
      }
      body {
        font-family: "PingFang SC", "Helvetica Neue", Arial, sans-serif;
      }
      .container {
        width: 100vw;
        min-height: 100vh;
        max-width: 480px;
        margin: 0 auto;
        background: #fff;
        border-radius: 28px 28px 0 0;
        box-shadow: 0 8px 32px rgba(22,93,255,0.08), 0 1.5px 8px rgba(0,0,0,0.03);
        padding: 24px 10px 18px 10px;
        display: flex;
        flex-direction: column;
        min-height: 100vh;
        box-sizing: border-box;
      }
      .input-area {
        width: 100%;
        min-height: 240px;
        font-size: 16px;
        border: 1px solid #e5e5e5;
        border-radius: 12px;
        padding: 12px;
        margin-bottom: 16px;
        resize: vertical;
        background: #fafbfc;
        box-sizing: border-box;
        transition: border 0.2s;
      }
      .input-area:focus {
        border: 1.5px solid #165dff;
        outline: none;
        background: #fff;
      }
      .btn-row {
        display: flex;
        gap: 12px;
        margin-bottom: 16px;
      }
      .btn {
        flex: 1 1 0;
        background: #165dff;
        color: #fff;
        border: none;
        border-radius: 12px;
        padding: 14px 0;
        font-size: 17px;
        cursor: pointer;
        transition: background 0.2s;
        box-shadow: 0 2px 8px rgba(22, 93, 255, 0.06);
        letter-spacing: 1px;
        min-width: 0;
      }
      .btn:active {
        background: #0e42a0;
      }
      .btn.screenshot-btn {
        background: #52c41a;
        margin-top: 24px;
        width: 200px;
      }
      .btn.screenshot-btn:active {
        background: #389e0d;
      }
      .btn.clear-btn {
        background: #ff4d4f;
      }
      .btn.clear-btn:active {
        background: #b71d1d;
      }
      .preview-area {
        background: #f6f6f6;
        border-radius: 14px;
        min-height: 90px;
        margin-bottom: 24px;
        padding: 16px 8px;
        box-shadow: 0 2px 8px rgba(22, 93, 255, 0.04);
        overflow-x: auto;
        font-size: 16px;
        word-break: break-all;
        max-width: 100%;
      }
      .footer {
        margin-top: auto;
        text-align: center;
      }
      .nav-row {
        display: flex;
        flex-wrap: nowrap;
        justify-content: space-between;
        align-items: flex-end;
        margin-bottom: 28px;
        margin-top: 4px;
        gap: 18px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
      }
      .nav-row::-webkit-scrollbar {
        display: none;
      }
      .nav-item {
        flex: 1 1 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        background: #f6f6f6;
        border-radius: 22px;
        padding: 18px 0 10px 0;
        margin: 0;
        font-size: 16px;
        box-shadow: 0 2px 10px rgba(22,93,255,0.06);
        transition: background 0.18s, box-shadow 0.18s, transform 0.12s;
        cursor: pointer;
        min-width: 0;
        user-select: none;
        touch-action: manipulation;
        position: relative;
      }
      .nav-item:active {
        background: #e6f0ff;
        box-shadow: 0 4px 16px rgba(22,93,255,0.10);
        transform: scale(0.97);
      }
      .nav-emoji {
        font-size: 34px;
        margin-bottom: 6px;
        display: block;
        filter: drop-shadow(0 2px 4px rgba(22,93,255,0.08));
      }
      .nav-text {
        font-size: 15px;
        color: #165dff;
        font-weight: 600;
        letter-spacing: 0.5px;
        margin-bottom: 6px;
      }
      .nav-dot {
        width: 8px;
        height: 8px;
        background: #165dff;
        border-radius: 50%;
        margin: 0 auto;
        margin-top: 2px;
        margin-bottom: 0;
        opacity: 0.7;
      }
      @media (max-width: 480px) {
        .container {
          border-radius: 0;
          padding: 12px 4px 10px 4px;
        }
        .btn {
          font-size: 16px;
          padding: 12px 0;
        }
        .preview-area {
          font-size: 15px;
          padding: 12px 4px;
        }
        .input-area {
          font-size: 15px;
          padding: 10px;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="nav-row">
        <div class="nav-item">
          <span class="nav-emoji">🏪</span>
          <span class="nav-text">成为平台商家</span>
          <span class="nav-dot"></span>
        </div>
        <div class="nav-item">
          <span class="nav-emoji">🧑‍💼</span>
          <span class="nav-text">成为平台从业者</span>
          <span class="nav-dot"></span>
        </div>
        <div class="nav-item">
          <span class="nav-emoji">ℹ️</span>
          <span class="nav-text">信息</span>
          <span class="nav-dot"></span>
        </div>
        <div class="nav-item">
          <span class="nav-emoji">👋</span>
          <span class="nav-text">联系我们</span>
          <span class="nav-dot"></span>
        </div>
      </div>
      <textarea id="htmlInput" class="input-area" placeholder="请输入HTML代码"><h1 style='color:#165DFF;'>Hello World</h1></textarea>
      <div class="btn-row">
        <button class="btn" id="previewBtn">预览</button>
        <button class="btn clear-btn" id="clearBtn">清空</button>
      </div>
      <div id="previewArea" class="preview-area"></div>
      <div class="footer">
        <button class="btn screenshot-btn" id="screenshotBtn">截图并下载</button>
      </div>
    </div>
    <script>
      const htmlInput = document.getElementById("htmlInput");
      const previewBtn = document.getElementById("previewBtn");
      const previewArea = document.getElementById("previewArea");
      const screenshotBtn = document.getElementById("screenshotBtn");
      const clearBtn = document.getElementById("clearBtn");

      previewBtn.onclick = function () {
        const html = htmlInput.value;
        // 新开窗口
        const win = window.open("", "_blank");
        // 写入完整HTML
        win.document.open();
        win.document.write(html);
        win.document.close();
        previewArea.innerHTML = html;
      };

      clearBtn.onclick = function () {
        htmlInput.value = "";
        previewArea.innerHTML = "";
      };

      screenshotBtn.onclick = function () {
        const html = htmlInput.value;
        previewArea.innerHTML = html;
        if (!previewArea.innerHTML.trim()) {
          alert("请先输入HTML并点击预览");
          return;
        }
        html2canvas(previewArea, { useCORS: true })
          .then((canvas) => {
            const url = canvas.toDataURL("image/png");
            const a = document.createElement("a");
            a.href = url;
            a.download = "preview.png";
            a.click();
          })
          .catch(() => {
            alert("截图失败");
          });
      };
    </script>
  </body>
</html>
